import { defineStore } from 'pinia'
import { ref } from 'vue'
import * as contentApi from '@/api/content'
import type { Content, ContentListParams, ContentListResponse } from '@/types'

export const useContentStore = defineStore('content', () => {
  const contentList = ref<Content[]>([])
  const loading = ref(false)
  const total = ref(0)

  // 获取内容列表
  const fetchContentList = async (params: ContentListParams): Promise<ContentListResponse> => {
    loading.value = true
    try {
      const response = await contentApi.getContentList(params)
      contentList.value = response.list
      total.value = response.total
      return response
    } finally {
      loading.value = false
    }
  }

  // 获取内容详情
  const fetchContentDetail = async (id: number) => {
    return await contentApi.getContentDetail(id)
  }

  // 创建内容
  const createContent = async (data: Partial<Content>) => {
    return await contentApi.createContent(data)
  }

  // 更新内容
  const updateContent = async (id: number, data: Partial<Content>) => {
    return await contentApi.updateContent(id, data)
  }

  // 删除内容
  const deleteContent = async (id: number) => {
    return await contentApi.deleteContent(id)
  }

  // 发布内容
  const publishContent = async (id: number) => {
    return await contentApi.publishContent(id)
  }

  // 下线内容
  const offlineContent = async (id: number) => {
    return await contentApi.offlineContent(id)
  }

  // 设置置顶
  const setContentTop = async (id: number, isTop: boolean) => {
    return await contentApi.setContentTop(id, isTop)
  }

  // 设置热门
  const setContentHot = async (id: number, isHot: boolean) => {
    return await contentApi.setContentHot(id, isHot)
  }

  // 批量操作
  const batchPublish = async (ids: number[]) => {
    return await contentApi.batchPublish(ids)
  }

  const batchOffline = async (ids: number[]) => {
    return await contentApi.batchOffline(ids)
  }

  const batchDelete = async (ids: number[]) => {
    return await contentApi.batchDelete(ids)
  }

  // 获取内容统计
  const fetchContentStats = async (params?: any) => {
    return await contentApi.getContentStats(params)
  }

  return {
    contentList,
    loading,
    total,
    fetchContentList,
    fetchContentDetail,
    createContent,
    updateContent,
    deleteContent,
    publishContent,
    offlineContent,
    setContentTop,
    setContentHot,
    batchPublish,
    batchOffline,
    batchDelete,
    fetchContentStats
  }
})